package myJs.tool

import myJs.api.Api
import slinky.web.html._

import scala.collection.immutable.SeqMap
import scala.scalajs.js.Date
import myJs.Implicits._
import myJs.myPkg._
import org.scalajs.dom.window.localStorage
import shared.pojo.Role

/**
 * Created by yz on 14/7/2022
 */
trait ProjectTool {

  val viewUrl = s"${Api.apiUrl}/panda/tool/viewImage"

  def imageUrl(id: String, fileName: String) = s"${viewUrl}?id=${id}&fileName=${fileName}"

  val freezerNumberMap = SeqMap("" -> "无") ++ (1 to 3).map(_.toString).toList.optionMap
  val freezerLayerNumberMap = SeqMap("" -> "无") ++ (1 to 10).map(_.toString).toList.optionMap

  def getRoleOp = {
    val roleStr = localStorage.getItem("role")
    if (roleStr == null) {
      None
    } else {
      Some(Role.withNameInsensitive(roleStr))
    }
  }

  def printClick = {
    PrintJS(
      PrintJsOptions(
        printable = "prepare_print_block",
        `type` = "html",
        style =
          s"""
             |#prepare_print_block .qrcodeBox {
             |    list-style: none;
             |    display: flex;
             |    flex-direction: column;
             |    align-items: center;
             |    justify-content: center;
             |    margin: 0;
             |    padding: 0;
             |    background-color: #fff;
             |    width: 120px;
             |    height: 240px;
             |}
             |#prepare_print_block .qrcodeBox:before {
             |    display: none;
             |}
             |#prepare_print_block{
             |    display: flex;
             |    width: 400px;
             |    flex-wrap: wrap;
             |}
             |
             |#prepare_print_block .qrcodeBox p {
             |    font-size: 20px !important;
             |    font-weight: bold;
             |}
             |
             |@media print {
             |    #prepare_print_block .qrcodeBox{
             |        page-break-after: always !important;
             |    }
             |
             |    #prepare_print_block {
             |        -webkit-print-color-adjust: exact;
             |        background-color: #fff;
             |        display: block !important;
             |        width: 190px !important;
             |    }
             |    @page {
             |        margin: 0;
             |        padding: 0;
             |    }
             |    .noprint {
             |        display: none;
             |    }
             |
             |    #prepare_print_block .qrcodeBox p {
             |        font-weight: bold !important;
             |    }
             |
             |}
             |""".stripMargin,
        targetStyles = List("*"),
        targetStyle = List("*"),
        font_size = "auto",
      )
    )
  }

  def freezerInfoFmt(v: ujson.Value, row: ujson.Obj, freezerInfoShow: String => () => Unit) = {
    button(className := "btn-link get_group_info", title := "查看",
      onClick := freezerInfoShow(row("id").str),
      i(className := "fa fa-search-plus"))()
  }

  def labFmt(v: ujson.Value, row: ujson.Obj, labShow: String => () => Unit) = {
    button(className := "btn-link get_group_info", title := "查看",
      onClick := labShow(row("id").str),
      i(className := "fa fa-search-plus"))()
  }

}
